package com.voltmao.webpro.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {

    public static Date now() {
        Calendar c = Calendar.getInstance(Locale.CHINA);
        return c.getTime();
    }

    private DateUtil() {

    }


    /**
     * @param date1
     * @param date2
     * @return
     * @throws ParseException
     */
    public static int getDaySpace(Date date1, Date date2) {

        Calendar calst = Calendar.getInstance();
        ;
        Calendar caled = Calendar.getInstance();

        calst.setTime(date1);
        caled.setTime(date2);

        //设置时间为0时
        calst.set(Calendar.HOUR_OF_DAY, 0);
        calst.set(Calendar.MINUTE, 0);
        calst.set(Calendar.SECOND, 0);
        caled.set(Calendar.HOUR_OF_DAY, 0);
        caled.set(Calendar.MINUTE, 0);
        caled.set(Calendar.SECOND, 0);
        //得到两个日期相差的天数
        int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst.getTime().getTime() / 1000)) / 3600 / 24;

        return days;
    }


    /**
     * 得到时间对象
     *
     * @param dateStr 时间的字符串
     * @param format  日期格式
     * @return
     */
    public static Date getDateForFormat(String dateStr, String format) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            return dateFormat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 得到指定时间的日期对象
     *
     * @param hour
     * @param second
     * @return
     */
    public static Date getEveryDate(int hour, int second) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, second);
        c.set(Calendar.SECOND, 0);
        Date date2 = c.getTime();
        return date2;
    }

    /**
     * 返回指定格式的字符串
     *
     * @param day
     * @param format
     * @return
     */
    public static String getDateStr(Date day, String format) {

        return new SimpleDateFormat(format).format(day);
    }

    /**
     * 得到当天的开0点时间字串
     *
     * @param day
     * @param format
     * @return
     */
    public static String getDayBegin(Date day, String format) {

        Calendar cal = new GregorianCalendar();
        cal.setTime(day);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 001);

        return new SimpleDateFormat(format).format(cal.getTime());
    }

    /**
     * 得到当天的结束时间字串
     *
     * @param day
     * @param format
     * @return
     */
    public static String getDayEnd(Date day, String format) {

        Calendar cal = new GregorianCalendar();
        cal.setTime(day);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.MILLISECOND, 999);

        return new SimpleDateFormat(format).format(cal.getTime());
    }

    /**
     * 得到日期字符串(偏移天数/月数)
     *
     * @param formatStr
     * @param calendarPy
     * @param num
     * @return
     */
    public static String getDateStr(String formatStr, int calendarPy, int num) {

        Date date = new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendarPy, num);//把日期往前减少一天，若想把日期向后推一天则将负数改为正数
        date = calendar.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat(formatStr);
        String dateString = formatter.format(date);

        return dateString;
    }

    /**
     * 改变日期
     *
     * @param date
     * @param calendarPy
     * @param num
     * @return
     */
    public static Date getDateByChange(Date date, int calendarPy, int num) {

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendarPy, num);//把日期往前减少一天，若想把日期向后推一天则将负数改为正数
        date = calendar.getTime();

        return date;
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return (int) between_days;
    }

    /**
     * 计算两个日期之间相差的小时数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static int hourBetween(Date startDate, Date endDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(startDate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(endDate);
        long time2 = cal.getTimeInMillis();
        long between_hour = (time2 - time1) / (1000 * 3600);

        return (int) between_hour;
    }


    /**
     * 是否大于第二时间
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isGreater(Date date1, Date date2) {

        try {
            return date1.getTime() <= date2.getTime();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return false;
    }


    /**
     * 得到
     *
     * @param date
     * @return
     */
    public static int getHourForDate(Date date, int filedType) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);

        return calendar.get(filedType);
    }

    /**
     * 得到日期间隔的列表
     *
     * @param smdate
     * @param bdate
     * @return
     */
    public static List<String> getEachDay(Date smdate, Date bdate, String format) {
        List<String> list = new LinkedList<String>();

        int betweenDay = daysBetween(smdate, bdate);
        Date pointDate = smdate;

        for (int i = 0, size = betweenDay; i < size; i++) {
            list.add(getDateStr(pointDate, format));
            pointDate = getDateByChange(pointDate, Calendar.DATE, 1);
        }

        return list;
    }

    /**
     * 得到日期分段的列表
     *
     * @param date
     * @param num
     * @return
     */
    public static List<String> getDayPartition(Date date, int num) {

        Date tmDate = getDateForFormat(getDayBegin(date, "yyyy-MM-dd"), "yyyy-MM-dd");
        List<String> listArr = new ArrayList<>();

        int minuteNum = (24 * 60) / num;
        for (int i = 0, size = num; i < size; i++) {
            listArr.add(getDateStr(tmDate, "yyyy-MM-dd HH:mm:ss"));
            tmDate = getDateByChange(tmDate, Calendar.MINUTE, minuteNum);
        }

        return listArr;
    }


    /**
     * 判断是否在时间段
     *
     * @param tStart
     * @param tEnd
     * @param t
     * @return
     */
    public static boolean isInZone(Date tStart, Date tEnd, Date t) {
        return tStart.getTime() <= t.getTime() && t.getTime() <= tEnd.getTime();
    }
}
